package com.example.liuke.guangguang;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;

public class Myhandler extends DefaultHandler {
	private String url;
	private String tagName;
	private ArrayList<String> content ;

	public Myhandler(ArrayList<String> content) {
		super();
		this.content = content;
	}

	/**
	 * 开始解析xml文件
	 */
	public void startDocument() throws SAXException {
		System.out.println("````````begin````````");
	}

	/**
	 * 解析xml文件结束
	 */
	public void endDocument() throws SAXException {
		System.out.println("````````end````````");
	}

	/**
	 * 开始解析标签
	 */
	public void startElement(String namespaceURI, String localName, String qName, Attributes attr) throws SAXException {
		tagName = localName;
		if (localName.equals("protype")) {
			// ��ȡ��ǩ��ȫ������
			for (int i = 0; i < attr.getLength(); i++) {
				System.out.println("startElement " + attr.getLocalName(i) + "=" + attr.getValue(i));
			}
		}
	}

	/**
	 * 解析标签结束
	 */
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
		// 在protype标签解析完之后，会打印出所有得到的数据,几个pretype，content的size就是几
		tagName = "";
		if (localName.equals("URL")) {
			this.printout();
			content.add(url);
		}
		url="";
	}

	/**
	 * 读取到内容
	 */
	public void characters(char[] ch, int start, int length) throws SAXException {
		if (tagName.equals("URL")) {
			url = new String(ch, start, length);
			
		} else if (tagName.equals("sex")) {
			// url = new String(ch, start, length);
		}	
	}

	// 打开一个标签的内容
	private void printout() {
		System.out.print("name: ");
		System.out.println(url);
		System.out.println();
	}
}